package com.nick.proxy.cglib;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * Created by xiaobao on 2017/7/13.
 */
public class CGlibTranslator implements MethodInterceptor {

    public Object getTranslator(Class clazz) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(clazz);
        enhancer.setCallback(this);
        return enhancer.create();
    }

    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("CGlib Translator start translating...");
        Object object = methodProxy.invokeSuper(o, objects);
        System.out.println("你好，我是尼克，来自中国。");
        System.out.println("CGlib Translator finished translating.");
        return object;
    }
}
